package net.flintx.array;

/**
 * Created by flintx on 2018/1/13.
 */
public class NonDecreasingArray {
    public boolean checkPossibility(int[] nums) {
        if (nums.length < 3) {
            return true;
        }
        int count = 0, pos = 0;
        for (int i = nums.length - 1; i > 0; i--) {
            nums[i] = nums[i] - nums[i - 1];
            if (nums[i] < 0) {
                count += 1;
                pos = i;
            }
        }
        if (count > 1) {
            return false;
        } else if (count == 1) {
            if (pos == nums.length - 1 || pos == 1 || nums[pos + 1] + nums[pos] >= 0 || nums[pos - 1] + nums[pos] >= 0)
                return true;
            else
                return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
        int[] nums = {3, 4, 2, 3};
        new NonDecreasingArray().checkPossibility(nums);
    }
}
